/*
 * dds_lib.c
 *
 *  Created on: Feb 12, 2013
 *      Author: justin
 */

#include "project.h"

#define FREQ_MUL 20e6f
#define PHSE_MUL 651.8986469f

uint16_t RESET_WD = 0x0100;
uint16_t START_WD = 0x2000;
uint16_t SET_F_WD = 0x2100;
uint16_t PHSE_MSK = 0xC000;
uint16_t FREQ_MSK = 0x4000;

ddsInit() {
	spiWriteShort(RESET_WD);
}

ddsStart() {
	spiWriteShort(START_WD);
}

ddsSetFrequency(uint32_t frequency) {
	uint32_t divisor = frequency * FREQ_MUL;
	spiWriteShort(SET_F_WD);
	spiWriteShort((divisor & 0x3fff) | FREQ_MSK);
	spiWriteShort((divisor >> 14) | FREQ_MSK);

}

void ddsSetPhase(uint16_t phase) {
	spiWriteShort((uint16_t) (phase * PHSE_MUL) | PHSE_MSK);
}
